package com.wantong.jianpu.viewmodels;

import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.wantong.jianpu.activitys.FaultDetailActivity;
import com.wantong.jianpu.activitys.TourDetailActivity;
import com.wantong.jianpu.adapters.DeviceListAdapter;
import com.wantong.jianpu.adapters.FaultRepairAdapter;
import com.wantong.jianpu.base.BaseResultBean;
import com.wantong.jianpu.beans.DeviceListBean;
import com.wantong.jianpu.beans.FaultListBean;
import com.wantong.jianpu.models.DeviceModel;
import com.wantong.jianpu.net.BaseObserver;
import com.wantong.jianpu.utils.Constants;

import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.DateUtil;
import me.goldze.mvvmhabit.utils.StringUtils;

/**
 * @author PACY
 * @date 2022/5/6
 * description:
 */
public class DeviceListViewModel extends BaseViewModel {
    public DeviceModel model;
    public DeviceListAdapter mAdapter;
    public SingleLiveEvent<String> date = new SingleLiveEvent<>();//选择的时间
    private int pageSize=20;
    private int pageNum=1;
    public SingleLiveEvent<Boolean> refreshEvent=new SingleLiveEvent<>();

    public DeviceListViewModel(@NonNull Application application) {
        super(application);
        model=new DeviceModel();
    }

    @Override
    protected void init() {
        super.init();
        initDate();
        initAdapter();
        loadData(true);
    }

    public void initDate() {
        date.setValue(DateUtil.getNowDate(DateUtil.DatePattern.ONLY_MONTH));
    }

    public void initAdapter(){
        mAdapter=new DeviceListAdapter();
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            loadData(false);
        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Bundle bundle=new Bundle();
            bundle.putString("id",mAdapter.getItem(position).getId());
            startActivity(TourDetailActivity.class,bundle);
        });

    }


    /**
     * 查询设备巡检列表
     */
    public void loadData(boolean isRefresh){
        if(isRefresh){
            pageNum=1;
            refreshEvent.setValue(true);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);

        model.getDeviceList(map, new BaseObserver<DeviceListBean>() {
            @Override
            public void onSuccess(BaseResultBean<DeviceListBean> t) {
                if(isRefresh){
                    if(t.getData().getRows().isEmpty()){
                        showEmpty();
                    }else{
                        showContent();
                        mAdapter.setNewInstance(t.getData().getRows());
                    }
                }else{
                    mAdapter.addData(t.getData().getRows());
                    mAdapter.getLoadMoreModule().loadMoreComplete();
                    if(t.getData().getRows().size()<pageSize){
                        mAdapter.getLoadMoreModule().loadMoreEnd();
                    }
                }
                pageNum++;
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mAdapter.getLoadMoreModule().loadMoreFail();
            }

            @Override
            public void onComplete() {
                super.onComplete();
                refreshEvent.setValue(false);
            }
        },getLifecycleProvider());
    }
}
